home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / util / observe / observabledict.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  7KB  |  224 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from observable import ObservableBase
  5. from weakref import WeakValueDictionary
  6. from collections import defaultdict
  7. from traceback import print_exc
  8. from util.primitives import better_ref
  9.  
  10. try:
  11.     import wx
  12.     from wx import EvtHandler
  13. except ImportError:
  14.     _usingWX = False
  15.  
  16. _usingWX = True
  17.  
  18. class Linked(object):
  19.     __slots__ = ('dict', 'thread', 'attr', 'ref')
  20.     
  21.     def __init__(self, dict, thread, attr, ref):
  22.         self.dict = dict
  23.         self.thread = thread
  24.         self.attr = attr
  25.         self.ref = ref
  26.  
  27.     
  28.     def unlink(self):
  29.         links = self.dict._links[self.thread]
  30.         if self.attr in links:
  31.             
  32.             try:
  33.                 links[self.attr].remove(self.ref)
  34.             except ValueError:
  35.                 pass
  36.             except:
  37.                 None<EXCEPTION MATCH>ValueError
  38.             
  39.  
  40.         None<EXCEPTION MATCH>ValueError
  41.  
  42.  
  43.  
  44. class DictWatcher(object):
  45.     
  46.     def __init__(self, srcdict, child_callback, *attrs):
  47.         srcdict.add_observer(self.on_dict_changed)
  48.         self.srcdict = srcdict
  49.         self.dictcopy = srcdict.copy()
  50.         self.child_args = [
  51.             child_callback] + list(attrs)
  52.         for key, child in srcdict.items():
  53.             child.add_observer(*self.child_args)
  54.         
  55.  
  56.     
  57.     def unbind_children(self):
  58.         for key, child in self.srcdict.items():
  59.             child.remove_observer(*self.child_args)
  60.         
  61.         self.srcdict.clear()
  62.         del self.srcdict
  63.  
  64.     
  65.     def on_dict_changed(self, src, attr, old, new):
  66.         if src != getattr(self, 'srcdict', None):
  67.             raise wx.PyDeadObjectError
  68.         
  69.         new = set(src.keys())
  70.         old = set(self.dictcopy.keys())
  71.         for newkey in new - old:
  72.             src[newkey].add_observer(*self.child_args)
  73.         
  74.         for oldkey in old - new:
  75.             self.dictcopy[oldkey].remove_observer(*self.child_args)
  76.         
  77.         self.dictcopy = src.copy()
  78.  
  79.  
  80.  
  81. class ObservableDict(dict, ObservableBase):
  82.     _dict_observers = WeakValueDictionary()
  83.     
  84.     def __init__(self, *args):
  85.         ObservableBase.__init__(self)
  86.         dict.__init__(self, *args)
  87.  
  88.     
  89.     def __setitem__(self, key, val):
  90.         old = self.get(key, None)
  91.         retval = dict.__setitem__(self, key, val)
  92.         self.notify(key, old, val)
  93.         return retval
  94.  
  95.     
  96.     def secret_set(self, key, val):
  97.         return dict.__setitem__(self, key, val)
  98.  
  99.     
  100.     def remove_dict_observer(self, dict_changed, child_changed):
  101.         watcher = self._dict_observers.pop((dict_changed, child_changed), None)
  102.         if watcher:
  103.             watcher.unbind_children()
  104.             del watcher
  105.         
  106.  
  107.     
  108.     def link(self, key, callback, callnow = True, obj = None, thread = 'gui'):
  109.         
  110.         try:
  111.             links = self._links
  112.         except AttributeError:
  113.             links = self._setup_links()
  114.  
  115.         
  116.         try:
  117.             thread_links = links[thread]
  118.         except KeyError:
  119.             thread_links = links[thread] = defaultdict(list)
  120.  
  121.         if obj is None:
  122.             obj = callback.im_self
  123.         
  124.         ref = better_ref(callback, obj = obj)
  125.         thread_links[key].append(ref)
  126.         if callnow:
  127.             callback(self[key])
  128.         
  129.         return Linked(self, thread, key, ref)
  130.  
  131.     
  132.     def _setup_links(self):
  133.         print 'in _setup_links for object at %s' % id(self)
  134.         links = { }
  135.         object.__setattr__(self, '_links', links)
  136.         self.add_observer(self._link_watcher)
  137.         return links
  138.  
  139.     
  140.     def _link_watcher(self, src, attr, old, new):
  141.         for thread_name, callbacks in self._links.items():
  142.             if attr in callbacks:
  143.                 
  144.                 def later(cbs = (callbacks[attr],)):
  145.                     for cb_ref in list(cbs):
  146.                         cb = cb_ref()
  147.                         if cb is None:
  148.                             
  149.                             try:
  150.                                 cbs.remove(cb)
  151.                             except ValueError:
  152.                                 pass
  153.                             except:
  154.                                 None<EXCEPTION MATCH>ValueError
  155.                             
  156.  
  157.                         None<EXCEPTION MATCH>ValueError
  158.                         
  159.                         try:
  160.                             cb(new)
  161.                         continue
  162.                         except Exception:
  163.                             print_exc()
  164.                             continue
  165.                         
  166.  
  167.                     
  168.  
  169.                 if thread_name == 'gui':
  170.                     wx.CallAfter(later)
  171.                 
  172.             thread_name == 'gui'
  173.         
  174.  
  175.     
  176.     def clear(self):
  177.         keys = self.keys()
  178.         old_vals = [ self[k] for k in keys ]
  179.         dict.clear(self)
  180.         self.notify()
  181.  
  182.     
  183.     def update(self, mapping):
  184.         keys = mapping.keys()
  185.         old_vals = [ self.get(k, None) for k in keys ]
  186.         new_vals = [ mapping.get(k) for k in keys ]
  187.         dict.update(self, mapping)
  188.         self.notifyall(keys, old_vals, new_vals)
  189.  
  190.     
  191.     def __delitem__(self, key):
  192.         old_val = self[key]
  193.         retval = dict.__delitem__(self, key)
  194.         self.notify(key, old_val, None)
  195.         return retval
  196.  
  197.     
  198.     def setdefault(self, k, x = None):
  199.         if k not in self:
  200.             retval = dict.setdefault(self, k, x)
  201.             self.notify(k, None, x)
  202.             return retval
  203.         else:
  204.             return self[k]
  205.  
  206.     
  207.     def pop(self, k, x = None):
  208.         if k in self:
  209.             val = self[k]
  210.             dict.pop(self, k, x)
  211.             self.notify(k, val, None)
  212.             return val
  213.         else:
  214.             return x
  215.  
  216.     
  217.     def popitem(self):
  218.         (k, v) = dict.popitem(self)
  219.         self.notify(k, v, None)
  220.         return (k, v)
  221.  
  222.  
  223. observable_dict = ObservableDict
  224.